// pages/mine/mine.js
import {
  Alert,
  Toast
} from 'wuss-weapp';
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabbar: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    app.editTabbar();
    this.setData({
      theme: app.globalData.theme,
    });
    app.setNavigationBarColor();
    this.getWxInfo();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.setData({
      wxUserInfo: app.globalData.wxUserInfo,
      yardId: app.globalData.yardId
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  /**
   * 登记授权
   */
  indexInfoEnrollment: function() {
    if (app.globalData.userInfo["user_id"]) {
      app.getUserInfo().then(result => {
        app.globalData.userInfo = result.data.data;
        if (app.globalData.userInfo["people_id"]) {
          app.isBindCommunity().then(result => {
            result.data.data.list.forEach((item, index) => {
              if (index == 0) {
                app.globalData.roomId = item.room_id;
                app.globalData.yardId = item.yard_id;
                app.globalData.areaCode = item.area_code;
                wx.setStorageSync('roomId', item.room_id);
                wx.setStorageSync('yardId', item.yard_id);
                wx.setStorageSync('areaCode', item.area_code);
              }
            });
            if (result.data.data.list.length > 0) { //存在房屋
              this.setData({
                yardId: app.globalData.yardId
              });
              wx.navigateTo({
                url: '/pages/mine/my_house/my_house',
              })
            } else { //不存在房屋
              app.wxGetRequest("/mine/searchApplyRecord", {
                people_id: app.globalData.userInfo["people_id"],
                page_number: 1,
                page_size: 10
              }).then(result => {
                if (result.data.data.list.length > 0) { //表明申请记录并失败了
                  wx.navigateTo({
                    url: '/pages/mine/my_house/add_house/add_house',
                  })
                } else {
                  wx.navigateTo({
                    url: '/pages/index/authorization_certification/authorization_certification',
                  })
                }
              });
            }
          });
        } else {
          wx.navigateTo({
            url: '/pages/index/authorization_certification/authorization_certification',
          })
        }
      });
    } else {
      wx.navigateTo({
        url: '/pages/mine/login/login',
      })
    }
  },
  /**
   * 设置
   */
  mineSetting: function() {
    app.isVerifiedLogin("/pages/mine/setting/setting");
  },
  /**
   * 我的房屋
   */
  mineMyHouse: function() {
    app.isVerifiedAuth("/pages/mine/my_house/my_house");
  },
  /**
   * 申请记录
   */
  mineApplyRecord: function() {
    app.isVerifiedAuth("/pages/mine/apply_record/apply_record");
  },
  /**
   * 我的车辆
   */
  mineMyCar: function() {
    app.isVerifiedAuth("/pages/mine/my_car/my_car");
  },
  /**
   * 身份管理
   */
  mineIdentityManagement: function() {
    app.isVerifiedAuth("/pages/mine/identity_management/identity_management");
  },
  /**
   * 人脸钥匙
   */
  mineFaceKey: function() {
    app.isVerifiedAuth().then(() => {
      if (!app.globalData.roomId) {
        app.isBindCommunity().then(result => {
          if (result.data.length > 0) {
            wx.navigateTo({
              url: "/pages/mine/face_key/face_key",
            })
          } else {
            app.alert("提示", "入住申请信息还在审核中，请到『申请记录』中去查看是否成功！");
          }
        });
      } else {
        wx.navigateTo({
          url: '/pages/mine/face_key/face_key',
        })
      }
    });
  },
  /**
   * 意见信箱
   */
  mineOpinionBox: function() {

  },
  /**
   * 智能家居
   */
  mineSmartHome: function() {
    app.isVerifiedAuth("/pages/mine/smart_home/smart_home");
  },
  /**
   * 便捷电话
   */
  mineConvenientPhone: function() {
    app.isVerifiedAuth().then(() => {
      if (!app.globalData.roomId) {
        app.isBindCommunity().then(result => {
          if (result.data.length > 0) {
            wx.navigateTo({
              url: "/pages/mine/convenient_phone/convenient_phone",
            })
          } else {
            app.alert("提示", "入住申请信息还在审核中，请到『申请记录』中去查看是否成功！");
          }
        });
      } else {
        wx.navigateTo({
          url: '/pages/mine/convenient_phone/convenient_phone',
        })
      }
    });
    // wx.navigateTo({
    //   url: '/pages/mine/convenient_phone/convenient_phone',
    // });
  },
  /**
   * 操作手册
   */
  mineOperationManual: function() {
    wx.navigateTo({
      url: '/common/page/article/article?fromPage=operationManualPage',
    })
  },
  /**
   * 我的收藏
   */
  mineMyCollect: function() {
    app.isVerifiedAuth("/pages/mine/my_collect/my_collect");
  },
  /**
   * 获取收藏数量
   */
  getCollectNumber: function() {
    app.wxGetRequest("/app-org/v1/forum_collect", {
      user_id: app.globalData.userInfo["id"],
      page_number: app.globalData.pageNumber,
      page_size: app.globalData.pageSize
    }).then(result => {
      this.setData({
        collectTotal: result.data.count
      });
    });
  },
  /**
   * 物业报修
   */
  minePropertyService: function() {
    app.isVerifiedAuth().then(() => {
      if (!app.globalData.roomId) {
        app.isBindCommunity().then(result => {
          if (result.data.length > 0) {
            wx.navigateTo({
              url: "/pages/mine/property_service/property_service",
            })
          } else {
            app.alert("提示", "入住申请信息还在审核中，请到『申请记录』中去查看是否成功！");
          }
        });
      } else {
        wx.navigateTo({
          url: '/pages/mine/property_service/property_service',
        })
      }
    });
  },
  /**
   * 我的积分
   */
  mineMyPoint: function() {
    app.isVerifiedLogin("/pages/mine/my_point/my_point");
  },
  /**
   * 访客记录
   */
  mineVisitorRecord: function() {
    app.isVerifiedAuth().then(() => {
      if (!app.globalData.roomId) {
        app.isBindCommunity().then(result => {
          if (result.data.length > 0) {
            wx.navigateTo({
              url: "/pages/mine/visitor_record/visitor_record",
            })
          } else {
            app.alert("提示", "入住申请信息还在审核中，请到『申请记录』中去查看是否成功！");
          }
        });
      } else {
        wx.navigateTo({
          url: '/pages/mine/visitor_record/visitor_record',
        })
      }
    });
  },
  /**
   * 大件物品
   */
  mineBigThing: function() {
    app.isVerifiedAuth().then(() => {
      if (!app.globalData.roomId) {
        app.isBindCommunity().then(result => {
          if (result.data.length > 0) {
            wx.navigateTo({
              url: "/pages/mine/big_thing/big_thing",
            })
          } else {
            app.alert("提示", "入住申请信息还在审核中，请到『申请记录』中去查看是否成功！");
          }
        });
      } else {
        wx.navigateTo({
          url: '/pages/mine/big_thing/big_thing',
        })
      }
    });
  },
  /**
   * 默认获取昵称和头像
   */
  getWxInfo: function() {
    if (!app.globalData.userInfo["user_id"]) {
      return;
    }
    app.getWxUserInfo().then((result) => {
      this.setData({
        wxUserInfo: result.data.data
      });
    })
  },
  /**
   * 社区工作人员
   */
  mineCommunityPeople: function() {
    wx.navigateTo({
      url: '/pages/mine/community_worker/community_worker',
    })
  }
})